n = 10
solutions = 4
sol = c('Perfect distribution','Nash product','Egalitarian solution','Utilitarian solution','Kalai-Smorodinsky solution')
h <- read.table("/Users/pawel/thesis/programs/ampl/u_A.txt", header=TRUE)
Pkast = c(116.48,249.05,390.13,536.28,686.92,838.712,990.504,1142.3,1294.09,1445.88)
sum_u = c(0, dim=solutions)
sum_e = c(0, dim=solutions)
gini = array(dimnames=colnames(h))
fairness = array(dimnames=colnames(h))
Pk_t  = array(0, dim=c(solutions,n))

###
impr = 0
###
s = max(colSums(h))

z = c(0, dim=n)
for (i in 1:n){
	z[i] = i*s/n
}
pdf(file='/Users/pawel/thesis/programs/R/output/centr_lorenz_A.pdf')

plot(z, type='l', ann=FALSE, lwd=2, lty=2)
 
lorenz <-function(solution, color)
{
	gini = 0
 	sorted=sort(solution, decreasing=FALSE)
 	Pk = c(0, dim=n)
 	Pk[1] = sorted[1]
	gini = z[1] - Pk[1]
	fairness = Pk[1]/Pkast[1]
 	for (i in 2:n){
 		Pk[i] = Pk[i-1] + sorted[i]
		gini = gini + z[i] +z[i-1] - Pk[i] - Pk[i-1]
		fairness = min(Pk[i-1]/Pkast[i-1],Pk[i]/Pkast[i])
 	}
	lines(Pk, col=color, lwd=2)
#Pk_t[name,1:n] = Pk
	gini = gini/(n*s)
	return(c(gini,fairness))
}	
 
j = 0
for(i in colnames(h)){
	j = j + 1
	xxx = lorenz(h[,j],j)
	gini[i] = xxx[1]
	fairness[i] = xxx[2]
}

title(xlab= 'Agent')
title(ylab= 'Cumulative utility')
title(main='Lorenz curves for scenario A', font.main=6)
legend(x='topleft',legend=sol,cex=1.2,col=c('black','green','red','blue','orange'),lty=c(2,1,1,1,1),lwd=2)

dev.off()

pdf(file='/Users/pawel/thesis/programs/R/output/centr_gini_A.pdf')
par(xpd=T, mar=par()$mar+c(0,0,0,10))
barplot(gini, ann=FALSE, col=pmatch(c('egal','util','nash','kalai'),colnames(h)))
#c('red','blue','orange','green'))
title(main='Gini coeficients for scenario A', font.main=6)
#legend(5,0.05,c('Egalitarian','Utilitarian','Kalai - Smorodinsky','Nash'),cex=1,fill=c('red','blue','orange','green'))
#legend(5,0.05,col=c('red','blue','orange','green'),cex=1,fill=sol[2:5])
dev.off()

#if(impr == 1){
#	pdf(file='/Users/pawel/thesis/programs/R/output/centr_fair_A.pdf')
#	par(xpd=T, mar=par()$mar+c(0,0,0,10))
#	barplot(fairness, ann=FALSE, col=c('red','blue','orange','green'))
#	title(main='Fairness ratios for scenario A', font.main=6)
#	legend(5,0.05,c('Egalitarian','Utilitarian','Kalai - Smorodinsky','Nash'),cex=1,fill=c('red','blue','orange','green'))
#	dev.off()
#}
